package com.hm.plugin.httpclient.util;

import java.util.HashMap;
import java.util.Map;

import com.hm.plugin.httpclient.model.ResponseCallBack;

/**
 * @author shishun.wang
 * @date 2016年6月28日 下午11:02:54
 * @version 1.0
 * @describe
 */
public class MainAppp {

	public static void main(String[] args) throws Exception {
		String baseUrl = "http://115.28.66.183:8080";
//		test(baseUrl);
//		115.28.66.183
//
//		String authorizationToken = "577524b30cf21ea9e802c11b";
//		upload(baseUrl,authorizationToken);
		
		MyHttpClient httpClientUtil = new MyHttpClient();
//		Map<String, Object> param = new HashMap<String, Object>();
//		{
//			param.put("name", "admin");
//			param.put("pwd", "password");
//		}
		
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8784/rest/v1/resbox/refresh/all/1");
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8784/rest/v1/book/57ab02a56054893075a0d6f7");
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8784/rest/v1/catalogue//57ab02a56054893075a0d6f7");
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8784/rest/v1/book/1/11");
		
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8784/rest/v1/topic/57aedab46054894da0543690/57b2b49f421aa97c2e000046");
		
		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://localhost:8784/rest/v1/topic/57b2db35eedcd036e46a642b/1111");
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get("http://10.0.0.203:8780/restapi/smartfactory/kb/topic/label/57ab0f196054895742e83589/EDITOR");
		System.out.println(responseCallBack.getResult());
		
//		ResponseCallBack<Object> loginCallBack = httpClientUtil.post(baseUrl + "/rest/login", param);
//		System.out.println(loginCallBack);
		
//		ResponseCallBack<Object> responseCallBack = httpClientUtil.get(baseUrl + "/rest/sys/user/57773d280cf202b6bf03a9aa");
//		System.out.println(responseCallBack);
//		
//		ResponseCallBack<Object> callBack = httpClientUtil.upload(baseUrl + "/rest/sys/file/IMAGE", "e:/test.png");
//		System.out.println(callBack);
		
		
	}
	
	/*private static void upload(String baseUrl ,String authorizationToken){
		baseUrl = baseUrl + "/rest/sys/file/IMAGE";
		
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		try {
			httpClient = HttpClients.createDefault();

			// 把一个普通参数和文件上传给下面这个地址 是一个servlet
			HttpPost httpPost = new HttpPost(baseUrl);
			{
				httpPost.setHeader("Client-AuthorizationToken", authorizationToken);
			}

			// 把文件转换成流对象FileBody
			FileBody bin = new FileBody(new File("e:/test.png"));

//			StringBody userName = new StringBody("Scott", ContentType.create("text/plain", Consts.UTF_8));
//			StringBody password = new StringBody("123456", ContentType.create("text/plain", Consts.UTF_8));

			HttpEntity reqEntity = MultipartEntityBuilder.create()
					// 相当于<input type="file" name="file"/>
					.addPart("files", bin).build();

					// 相当于<input type="text" name="userName" value=userName>
//					.addPart("userName", userName).addPart("pass", password).build();

			httpPost.setEntity(reqEntity);

			// 发起请求 并返回请求的响应
			response = httpClient.execute(httpPost);

			System.out.println("The response value of token:" + response.getFirstHeader("token"));

			// 获取响应对象
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				// 打印响应长度
				System.out.println("Response content length: " + resEntity.getContentLength());
				// 打印响应内容
				System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
			}

			// 销毁
			EntityUtils.consume(resEntity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}*/

	/*private static void test(String baseUrl) throws Exception {
		HttpClientUtil httpClientUtil = HttpClientUtil.getInstance();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		{
			params.add(new BasicNameValuePair("name", "admin"));
			params.add(new BasicNameValuePair("pwd", "password"));
		}
		ResponseCallBack<Object> loginCallBack = httpClientUtil.post(baseUrl + "/rest/login",
				new UrlEncodedFormEntity(params), null);

		// 获取当前操作用户权限身份认证（当前操作用户id，后期会对其加密处理）
		String authorizationToken = (String) loginCallBack.getResult();
		System.out.println(authorizationToken);

		// 测试获取表单提交token
		ResponseCallBack<Object> formToken = httpClientUtil.get(baseUrl + "/rest/token/" + System.currentTimeMillis(),
				authorizationToken);
		System.out.println(formToken.toString());

		// 测试获取用户信息
		ResponseCallBack<Object> userInfo = httpClientUtil.get(baseUrl + "/rest/sys/user/" + authorizationToken,
				authorizationToken);
		System.out.println(userInfo);

		// 保存用户信息
		SysUserInfoModel model = new SysUserInfoModel();
		{
			model.setName("张三32");
			model.setPwd("123");
			model.setMobile("13582356983");
			model.setAlias("小三");
		}
		StringEntity entity = new StringEntity(JSONObject.toJSONString(model), "utf-8");// 解决中文乱码问题
		{
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
		}
		ResponseCallBack<Object> saveUserInfo = httpClientUtil.post(baseUrl + "/rest/sys/user", entity,
				authorizationToken);

		String addUserId = (String) saveUserInfo.getResult();
		System.out.println(addUserId);

		// 获取刚刚添加的用户信息
		System.out.println(httpClientUtil.get(baseUrl + "/rest/sys/user/" + addUserId, authorizationToken));
	}*/
}
